import oss2
import uuid
import os
from datetime import datetime

from django.conf import settings

auth = oss2.AuthV4(settings.OSS_CONFIG['access_key_id'], settings.OSS_CONFIG['access_key_secret'])

region = "cn-beijing"

def get_extension(src):
    return os.path.splitext(src)[1].lstrip('.')

def upload_to_oss(src, bucket_name, directory):
    bucket = oss2.Bucket(auth, settings.OSS_CONFIG['endpoint'], bucket_name, region=region)
    extension = get_extension(src)
    current_date = datetime.now()
    filename = str(uuid.uuid4()).replace('-', '') + '.' + extension
    file_path = directory + "/" + current_date.strftime("%Y-%m-%d") + "/" + filename
    bucket.put_object_from_file(file_path, src)
    return "/" + file_path, filename
